<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<!--					<jk-button-->
					<!--						type="warning"-->
					<!--						:loading="exportLoading"-->
					<!--						icon="el-icon-upload2"-->
					<!--						size="small"-->
					<!--						class="item-gap"-->
					<!--						@click="onExportExcel"-->
					<!--					>导出</jk-button>-->
				</div>
				<div class="flex-end-center-wrap">
					<div class="flex-end-center-wrap">
						<jk-select v-model="searchParams.workshopId" :option-list="workshopList" clearable placeholder="区域" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
						<jk-date-picker v-model="searchParams.belongDate" :clearable="false" type="daterange" style="width: 280px;" placeholder="日期" :picker-options="pickerOptions" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-date-picker>
					</div>
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table
				ref="frontGroupSalaryMonthReportTable"
				:footer-cell-class-name="footerCellClassName3"
				show-footer
				:loading="tableLoading"
				:span-method="mergeColMethod"
				:footer-method="footerMethod"
				border
				:stripe="false"
				:data="tableData"
				:height="tableHeight + 30"
			>
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.key"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
				</vxe-column>
			</jk-table>
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatMonth, formatDate, tipsBar } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import { workforceManagementShiftGetByTime } from '../../../api/schedule/shift';
    import { exportExcelMerge } from '../../../api/commont';
    import XEUtils from 'xe-utils';
    import { iotMachine } from '../../../api/energy/energyWastage';
    import { systemUserColumnConfigGet } from '@/api/system/column-config';
    export default {
        name: 'EnergyWastage',
        components: {},
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                apiItem: {
                    'page': iotMachine
                },
                totalData: {
                    totalOutputM: 0,
                    totalOutputKg: 0
                },
                tableColumnList: [],
                processList: [],
                tableData: [],
                dataId: null,
                workshopList: [],
                tableLoading: true,
                exportLoading: false,
                count: 0,
                searchParams: {
                    belongDate: [],
                    workshopId: null
                }
            };
        },
        mounted() {
            this.workshopList = this.$store.state.user.userWorkshop.workshopList;
            if (!this.searchParams.workshopId) {
                this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
            }
            this.getDependentData();
        },
        methods: {
            // getMenuColumnListRequest() {
            //     return systemUserColumnConfigGet({ path: 'EnergyWastage', filterShow: true }).then(res => {
            //         if (res.data.status === 200) {
            //             this.tableColumnList = res.data.res.columnConfigList && res.data.res.columnConfigList.length ? res.data.res.columnConfigList[0].itemList : [];
            //         }
            //     });
            // },
            footerCellClassName3({ $rowIndex, column }) {
                if ($rowIndex !== 0 && column.field === 'all') { // 假设对所有非表头行且字段为'all'的列应用加粗
                    return 'font-bold';
                }
            },
            footerMethod({ columns, data }) {
                return [
                    columns.map((column, columnIndex) => {
                        if (columnIndex === 0) {
                            // return `合计: ${this.totalData.totalOutputM || 0}M; ${this.totalData.totalOutputKg || 0}KG`;
                            return `合计`;
                        }
                        if (column.property !== 'belongDate') {
                            // if (column.property.indexOf('Kg') !== -1) {
                            //     return XEUtils.sum(data, column.property) + 'KG';
                            // } else {
                            //     return XEUtils.sum(data, column.property) + 'M';
                            // }
                            return XEUtils.sum(data, column.property);
                        }
                        return null;
                    })
                ];
            },
            mergeColMethod({ row, _rowIndex, column, visibleData }) {
                const prevRow = visibleData[_rowIndex - 1];
                let nextRow = visibleData[_rowIndex + 1];
                if (column.property === 'belongDate') {
                    if (prevRow && prevRow.belongDate === row.belongDate) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.belongDate === row.belongDate) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                }
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            async getDependentData() {
                this.tableLoading = true;
                if (this.searchParams.belongDate.length === 0) {
                    this.searchParams.belongDate = [formatDate(Date.now()), formatDate(Date.now())];
                }
                await this.getListRequest();
                this.tableLoading = false;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = { ...this.searchParams };
                if (params.belongDate) {
                    params.endDate = params.belongDate[1];
                    params.startDate = params.belongDate[0];
                }
                delete params.belongDate;
                this.tableData = [];
                return this.apiItem['page'](params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableColumnList = res.data.res.tableHeader ? res.data.res.tableHeader.map(x => {
                            if (x.key === 'localDate') {
                                return {
                                    title: x.value,
                                    key: x.key,
                                    align: x.align ? x.align : 'left',
                                    width: x.width ? x.width : '100'
                                };
                            } else {
                                return {
                                    title: x.value,
                                    key: x.key,
                                    align: x.align ? x.align : 'right',
                                    width: x.width ? x.width : '150'
                                };
                            }
                        }) : [];
                        this.tableData = res.data.res.tableData;
                        this.$nextTick(() => {
                            // 修复底部每月1号合计计算错误的问题
                            this.$refs.frontGroupSalaryMonthReportTable.$refs.xTable.updateData();
                        });
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.searchParams.count = 1;
                this.getListRequest();
            },
            formatJson(filterVal, jsonData) {
                return jsonData.map(v => filterVal.map(j => {
                    return v[j];
                }));
            }
        }
    };
</script>
<style scoped>
.machine-group-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-left: solid 1px #eee;
  border-top: solid 1px #eee;
  width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
  flex-shrink: 0;
}
font-bold {
  font-weight: bold;
}
/deep/.vxe-table--footer{
    font-weight: 600;
    font-size: 14px;
}

</style>
